package br.fagoc.easyds.ui.jgraph;


import br.fagoc.easyds.util.JGraphFactory;
import br.fagoc.easyds.util.Delay;

import com.mxgraph.model.mxCell;
import com.mxgraph.model.mxICell;
import com.mxgraph.view.mxGraph;

public class GraphCellManipulator {

	private mxGraph graph;
	
	public GraphCellManipulator(mxGraph graph){
		this.graph = graph;
	}

	public mxCell createRectangularCell(String value, double xAxis, double yAxis){
		mxCell novaCelula = (mxCell) graph.createVertex(graph.getDefaultParent(), null, value, xAxis, yAxis, 80, 50, JGraphFactory.RECTANGULAR_STYLE);
		return novaCelula;
	}

	public mxCell drawRectangularCell(String value, double xAxis, double yAxis){
		mxCell novaCelula = (mxCell) graph.insertVertex(graph.getDefaultParent(), null, value, xAxis, yAxis, 80, 50, JGraphFactory.RECTANGULAR_STYLE);
		return novaCelula;
	}
	
	public void drawCell(mxCell cell){
		graph.getModel().add(graph.getDefaultParent(), cell, 0);
	}

	public void removeCell(mxCell graphCell){		
		graph.getModel().remove(graphCell);
	}

	public void blinkCell(mxCell graphCell){
		graph.setCellStyle(JGraphFactory.RECTANGULAR_STYLE_HIGHLIGHTED, new Object[]{graphCell});
		graph.repaint();
		Delay.applyGreaterDelay();
		
		graph.setCellStyle(JGraphFactory.RECTANGULAR_STYLE, new Object[]{graphCell});
		graph.repaint();
		Delay.applyGreaterDelay();
		
		graph.setCellStyle(JGraphFactory.RECTANGULAR_STYLE_HIGHLIGHTED, new Object[]{graphCell});
		graph.repaint();
		Delay.applyGreaterDelay();
		
		graph.setCellStyle(JGraphFactory.RECTANGULAR_STYLE, new Object[]{graphCell});
		graph.repaint();
	}
	
	public void horizontalTranslation(mxCell graphCell, double xAxisDestination){		
		double xAxis = graphCell.getGeometry().getX();

		if(xAxis != xAxisDestination){
			if( (xAxis < xAxisDestination && xAxis + 10 >= xAxisDestination) || (xAxis > xAxisDestination && xAxis - 10 <= xAxisDestination))
				graphCell.getGeometry().setX(xAxisDestination);
			else if(xAxis < xAxisDestination)
				graphCell.getGeometry().setX(xAxis + 10);
			else if(xAxis > xAxisDestination)
				graphCell.getGeometry().setX(xAxis - 10);

			graph.getModel().add(graph.getDefaultParent(), graphCell, 0);
			graph.refresh();
			Delay.applyDelay();

			horizontalTranslation(graphCell, xAxisDestination);
		}
	}
	
	public void horizontalTranslation(mxCell graphCell, double xAxis, double yAxis, double xAxisDestination){	
		if(xAxis != xAxisDestination){
			if( (xAxis < xAxisDestination && xAxis + 10 >= xAxisDestination) || (xAxis > xAxisDestination && xAxis - 10 <= xAxisDestination))
				xAxis = xAxisDestination;
			else if(xAxis < xAxisDestination)
				xAxis = xAxis + 10;
			else if(xAxis > xAxisDestination)
				xAxis = xAxis - 10;

			graphCell.getGeometry().setX(xAxis);
			graphCell.getGeometry().setY(yAxis);
			
			graph.getModel().add(graph.getDefaultParent(), graphCell, 0);
			graph.refresh();
			Delay.applyDelay();

			horizontalTranslation(graphCell, xAxisDestination);
		}
	}

	public void verticalTranslation(mxCell graphCell, double yAxisDestination){
		double yAxis = graphCell.getGeometry().getY();
		
		if(yAxis != yAxisDestination){
			if( (yAxis < yAxisDestination && yAxis + 10 >= yAxisDestination) || (yAxis > yAxisDestination && yAxis - 10 <= yAxisDestination))
				graphCell.getGeometry().setY(yAxisDestination);
			else if(yAxis < yAxisDestination)
				graphCell.getGeometry().setY(yAxis + 10);
			else if(yAxis > yAxisDestination)
				graphCell.getGeometry().setY(yAxis - 10);
			
			graph.getModel().add(graph.getDefaultParent(), graphCell, 0);
			graph.refresh();

			Delay.applyDelay();

			verticalTranslation(graphCell, yAxisDestination);
		}
	}
	
	public void verticalTranslation(mxCell graphCell, double xAxis, double yAxis, double yAxisDestination){
		if(yAxis != yAxisDestination){
			if( (yAxis < yAxisDestination && yAxis + 10 >= yAxisDestination) || (yAxis > yAxisDestination && yAxis - 10 <= yAxisDestination))
				yAxis = yAxisDestination;
			else if(yAxis < yAxisDestination)
				yAxis = yAxis + 10;
			else if(yAxis > yAxisDestination)
				yAxis = yAxis - 10;

			graphCell.getGeometry().setY(xAxis);
			graphCell.getGeometry().setY(yAxis);
			
			graph.getModel().add(graph.getDefaultParent(), graphCell, 0);
			graph.refresh();

			Delay.applyDelay();

			verticalTranslation(graphCell, yAxisDestination);
		}
	}
}
